package com.zpz.framework.zpzoauth.web;

import com.github.pagehelper.PageInfo;
import com.zpz.framework.zpzoauth.common.result.ZpzOauthResultMsg;
import com.zpz.framework.zpzoauth.common.result.ZpzOauthResultStatus;
import com.zpz.framework.zpzoauth.common.utils.ZpzOauthRequestUtil;
import com.zpz.framework.zpzoauth.pojo.po.OauthClientDetails;
import com.zpz.framework.zpzoauth.service.ZpzOauthFrameRoleMenuService;
import com.zpz.framework.zpzoauth.service.ZpzOauthFrameRoleService;
import com.zpz.framework.zpzoauth.pojo.qo.*;
import com.zpz.framework.zpzoauth.pojo.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zhangpanzhi
 * @description 角色管理
 * @create 2019-07-04 16:35
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping(value = "/zpzOauthRole")
public class ZpzOauthRoleController {

    @Resource
    private ZpzOauthFrameRoleService zpzOauthFrameRoleService;
    @Resource
    private ZpzOauthRequestUtil zpzOauthRequestUtil;
    @Resource
    private ZpzOauthFrameRoleMenuService zpzOauthFrameRoleMenuService;

    @ApiOperation(value = "根据用户编码获取用户角色编码信息（100005） |负责人：zhangpanzhi",
            notes = "  \n 数据安全：无," +
                    "  \n返回码：0（OK），")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "getRoleCodeByUserCodeListQo", value = "参数", paramType = "body", dataType = "GetRoleCodeByUserCodeListQo", required = true
            )})
    @PostMapping(value = "/getRoleCodeByUserCodeList")
    public ZpzOauthResultMsg<List<String>> getRoleCodeByUserCodeList(HttpServletRequest request, @RequestBody GetRoleCodeByUserCodeListQo getRoleCodeByUserCodeListQo) throws Exception{
        return zpzOauthFrameRoleService.getRoleCodeByUserCode(request,getRoleCodeByUserCodeListQo);
    }

    @ApiOperation(value = "添加角色信息（100012）|负责人：zhangpanzhi" ,notes = "数据安全：" +
            "返回码：  \n" +
            "  \n0（操作成功-OK，data为[1：添加成功，0：添加失败]）  " +
            "\n38000（角色名称不能为空） \n38001（该角色名称已存在）" +
            "\n38003（角色添加失败）\n38006（角色状态不能为空）" +
            "\n38007（角色状态不合法）"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addRoleInfoQo", value = "参数", paramType = "body", dataType = "AddRoleInfoQo", required = true
            )})
    @PostMapping("/addRole")
    public ZpzOauthResultMsg<Integer> addRole(HttpServletRequest request,@RequestBody AddRoleInfoQo addRoleInfoQo) throws Exception {
        if (StringUtils.isBlank(addRoleInfoQo.getRoleName())) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_NAME_NOT_NULL_38000);
        }
        return zpzOauthFrameRoleService.addRole(request, addRoleInfoQo);
    }

    @ApiOperation(value = "修改角色信息（100013）|负责人：zhangpanzhi" ,notes = "数据安全：" +
            "返回码：  \n" +
            "\n0（操作成功-OK）  \n38000（角色名称不能为空） " +
            "\n38001（该角色名称已存在）  \n38002（角色编码不能为空）" +
            "\n38004（角色更新失败）\n38006（角色状态不能为空）" +
            "\n38007（角色状态不合法"
    )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modifyRoleInfoQo", value = "参数", paramType = "body", dataType = "ModifyRoleInfoQo", required = true
            )})
    @PostMapping("/modifyRole")
    public ZpzOauthResultMsg<Integer> updateRole(HttpServletRequest request, @RequestBody ModifyRoleInfoQo modifyRoleInfoQo) throws Exception {
        if (StringUtils.isBlank(modifyRoleInfoQo.getRoleName())){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_NAME_NOT_NULL_38000);
        }
        if (modifyRoleInfoQo.getStatus() == null) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_CODE_STATUS_NOT_NULL_38006);
        }
        if (modifyRoleInfoQo.getStatus() > 1 || modifyRoleInfoQo.getStatus() < 0) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_CODE_STATUS_INVAILD_38007);
        }
        if (StringUtils.isBlank(modifyRoleInfoQo.getRoleCode())){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_CODE_NOT_NULL_38002);
        }
        return zpzOauthFrameRoleService.editRole(request,modifyRoleInfoQo);
    }

    @ApiOperation(value = "根据角色代码获取对应的角色信息（100014）|适用于（）|负责人：zhangpanzhi" ,notes = "数据安全：  \n" +
            "返回码：  \n" +
            "  \n0（操作成功-OK） \n38002（角色编码不能为空） \n1(查询数据为空)   \n")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "selectRoleInfoQo", value = "参数", paramType = "body", dataType = "SelectRoleInfoQo", required = true)})
    @PostMapping("/getRoleByRoleCode")
    public ZpzOauthResultMsg<GetRoleInfoVo> getRoleByRoleCode(HttpServletRequest request, @RequestBody SelectRoleInfoQo selectRoleInfoQo) throws Exception {

        if(StringUtils.isBlank(selectRoleInfoQo.getRoleCode())){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_CODE_NOT_NULL_38002);
        }
        return zpzOauthFrameRoleService.getRoleInfoByCode(selectRoleInfoQo.getRoleCode());
    }

    @ApiOperation(value = "查询角色下拉列表信息（100015）|适用于（）|负责人：zhangpanzhi",notes ="数据安全：无  \n" +
            "返回码：    \n" + "0（获取信息成功-OK）")
    @PostMapping("/getRoleDropdownList")
    public ZpzOauthResultMsg<List<RoleDropdownListVo>> getRoleDropdownList(HttpServletRequest request, @RequestBody GetClientQo getClientQo) throws Exception{
        return zpzOauthFrameRoleService.getRoleDropdownList(request,getClientQo);
    }

    @ApiOperation(value = "根据角色编码获取角色菜单信息集合（100016） |适用于（） |负责人：zhangpanzhi",
            notes = "  \n 数据安全：无," +
                    "  \n 请求体：roleCode为角色编码，不是主键ID" +
                    "  \n返回码：0（OK），" +
                    "  \n   角色编码不能为空:38002  ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleMenuInfoQo", value = "参数", paramType = "body", dataType = "RoleMenuInfoQo", required = true
            )})
    @PostMapping("/getRoleMenuListByRoleCode")
    public ZpzOauthResultMsg<List<GetRoleMenuVo>> getRoleMenuListByRoleCode(HttpServletRequest request, @RequestBody RoleMenuInfoQo roleMenuInfoQo) throws Exception {
        if (StringUtils.isBlank(roleMenuInfoQo.getRoleCode())) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_CODE_NOT_NULL_38008);
        }
        return zpzOauthFrameRoleMenuService.getRoleMenuListByRoleCode(request, roleMenuInfoQo.getRoleCode());
    }

    @ApiOperation(value = "修改角色启用状态（100017）|适用于（）|负责人：zhangpanzhi",notes = "数据安全： \n" +
            "返回码：  \n" +
            "  \n0（操作成功-OK） \n38002（角色编码不能为空） \n38006(角色状态不能为空)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleStateQo",value = "修改角色启用状态参数",paramType = "body",dataType = "RoleStateQo",required = true),
    })
    @RequestMapping(value = "/updateRoleState",method = RequestMethod.POST)
    public ZpzOauthResultMsg<Boolean> updateRoleState(HttpServletRequest request,@RequestBody RoleStateQo roleStateQo) throws Exception{
        try{
            if(StringUtils.isBlank(roleStateQo.getRoleCode())){
                return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_CODE_NOT_NULL_38002);
            }
            if(StringUtils.isBlank(roleStateQo.getStatus())){
                return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_CODE_STATUS_NOT_NULL_38006);
            }
            return zpzOauthFrameRoleService.updateRoleState(request,roleStateQo);
        }catch (Exception e){
            e.printStackTrace();
            return ZpzOauthResultMsg.error();
        }
    }

    @ApiOperation(value = "查询角色管理列表（100018）|适用于（）|负责人：zhangpanzhi",notes = "数据安全：   \n" +
            "返回码：  \n" +
            "  \n0（操作成功-OK） \n38012（角色管理列表查询失败）")
    @RequestMapping(value ="/selectRoleMangePageList",method = RequestMethod.POST)
    public ZpzOauthResultMsg<PageInfo<RoleManageVo>> selectRoleMangePageList(HttpServletRequest request, @RequestBody RoleManageQo roleManageQo){
        return zpzOauthFrameRoleService.selectRoleMangePageList(request,roleManageQo);
    }

    @ApiOperation(value = "删除角色相关信息（100019）|适用于（）|负责人：zhangpanzhi",notes = "数据安全：  \n" +
            "返回码：  \n" +
            "  \n0（操作成功-OK） \n38008（角色代码不能为空） \n38009(角色信息删除失败)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deleteRoleInfoQo",value = "删除角色相关信息",paramType = "body",dataType = "DeleteRoleInfoQo",required = true),
    })
    @RequestMapping(value = "/deleteRoleInfo",method = RequestMethod.POST)
    public ZpzOauthResultMsg<Boolean> deleteRoleInfo(HttpServletRequest request,@RequestBody DeleteRoleInfoQo deleteRoleInfoQo){
        try{
            if(deleteRoleInfoQo.getRoleCode()==null){
                return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_CODE_NOT_NULL_38008);
            }
            return zpzOauthFrameRoleService.deleteRoleInfo(request,deleteRoleInfoQo);
        }catch (Exception ex){
            ex.printStackTrace();
            return ZpzOauthResultMsg.error();
        }

    }

    @ApiOperation(value = "保存角色菜单权限设置（100020） |适用于（） |负责人：zhangpanzhi",
            notes = "  \n 数据安全：无," +
                    "  \n 请求体：roleCode为角色编码，不是主键ID" +
                    "  \n返回码：0（OK），" +
                    "  \n   角色编码不能为空:38002  \n 菜单权限集合不能为空：38013")
    @PostMapping(value = "/saveRoleMenu")
    public ZpzOauthResultMsg<Boolean> saveRoleMenu(HttpServletRequest request,@RequestBody SaveRoleMenuAccessQo saveRoleMenuAccessQo) {
        if (StringUtils.isBlank(saveRoleMenuAccessQo.getRoleCode())) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.ROLE_CODE_NOT_NULL_38008);
        }
        OauthClientDetails client = zpzOauthRequestUtil.getClient(saveRoleMenuAccessQo.getClientId());
        if (client.getAccessType()==0||client.getAccessType()==1){
            return zpzOauthFrameRoleMenuService.saveRoleMenu(request, saveRoleMenuAccessQo.getRoleCode(),saveRoleMenuAccessQo.getClientId(), saveRoleMenuAccessQo.getRoleMenuQoList());
        }else if (client.getAccessType()==2){
            return zpzOauthFrameRoleMenuService.saveDepartmentMenu(request, saveRoleMenuAccessQo.getRoleCode(),saveRoleMenuAccessQo.getClientId(), saveRoleMenuAccessQo.getRoleMenuQoList());
        }
        return ZpzOauthResultMsg.error();
    }

}
